package com.etang.a2020d1021.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.etang.a2020d1021.R;
import com.etang.a2020d1021.tools.AppConfig;

public class BaseFragment extends Fragment {

    private TextView tv_temp, tv_hum, tv_press, tv_smo, tv_gas, tv_ill, tv_co, tv_pm, tv_per;
    private ImageView iv_per_state;
    private boolean is_per_on = false;
    private int number = 0;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_base, null, false);
        initView(view);
        handler.post(time);
        return view;
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            tv_temp.setText(String.valueOf(AppConfig.temp));
            tv_hum.setText(String.valueOf(AppConfig.hum));
            tv_smo.setText(String.valueOf(AppConfig.smo));
            tv_gas.setText(String.valueOf(AppConfig.gas));
            tv_press.setText(String.valueOf(AppConfig.press));
            tv_ill.setText(String.valueOf(AppConfig.ill));
            tv_pm.setText(String.valueOf(AppConfig.pm));
            tv_co.setText(String.valueOf(AppConfig.co));
            if (AppConfig.per == 1) {
                tv_per.setText("有人");
                is_per_on = true;
            } else {
                tv_per.setText("无人");
                is_per_on = false;
            }
            if (is_per_on) {
                if (number % 2 == 0) {
                    iv_per_state.setBackgroundResource(R.drawable.ic_per_off);
                } else {
                    iv_per_state.setBackgroundResource(R.drawable.ic_per_on);
                }
            }
            handler.postDelayed(time, 1000);
        }
    };
    Runnable time = new Runnable() {
        @Override
        public void run() {
            number++;
            Message msg = handler.obtainMessage();
            handler.sendMessage(msg);
        }
    };


    private void initView(View view) {
        tv_co = (TextView) view.findViewById(R.id.tv_co);
        tv_gas = (TextView) view.findViewById(R.id.tv_gas);
        tv_hum = (TextView) view.findViewById(R.id.tv_hum);
        tv_ill = (TextView) view.findViewById(R.id.tv_ill);
        tv_per = (TextView) view.findViewById(R.id.tv_per);
        tv_pm = (TextView) view.findViewById(R.id.tv_pm);
        tv_press = (TextView) view.findViewById(R.id.tv_press);
        tv_smo = (TextView) view.findViewById(R.id.tv_smo);
        tv_temp = (TextView) view.findViewById(R.id.tv_temp);
        iv_per_state = (ImageView) view.findViewById(R.id.iv_per);
    }
}
